<?php

namespace app\user\logic;

use app\common\exception\LogicException;
use app\model\FactoryApply;

class FactoryApplyLogic
{
    protected FactoryApply $model;
    protected int $user_id;

    public function __construct()
    {
        $this->model = new FactoryApply();
        $this->user_id = request()->user_info['user_id'];
    }

    /**
     * @Description 新增
     * @author: zj
     * @param array $param
     * @time 2024/03/29 11:34:15
     * @return true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(array $param)
    {
        $exist = $this->model->checkNameIsExist($this->user_id,$param['name']);
        if ($exist) {
            throw new LogicException('猪厂已存在');
        }

        $this->model->insert([
            'user_id' => $this->user_id,
            'name' => $param['name'],
            'address' => $param['address'],
            'contacts' => $param['contacts'],
            'phone' => $param['phone'],
            'create_time' => request()->time(),
            'update_time' => request()->time(),
        ]);

        return true;
    }

    /**
     * @Description 列表
     * @author: zj
     * @param array $param
     * @time 2024/03/29 11:50:09
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getList(array $param)
    {
        $query = $this->model->field('id,name,address,contacts,phone,status,create_time')
            ->where('user_id',$this->user_id)
            ->where('delete_time',$this->model::IS_DELETE_NO);
        if ($param['name']) {
            $query->where('name','like','%'.trim($param['name']).'%');
        }
        if ($param['status']) {
            $query->where('status',$param['status']);
        }

        return $query->order('id DESC')
            ->paginate([
                'list_rows' => $param['page_size'],
                'page' => $param['page']
            ])
            ->toArray();
    }

}